from flask import Flask,jsonify,request,Blueprint,session
from common.res import (getRes,getErrorRes,getSuccessRes,getDataRes)
from common.sqlInjectionSecurity import sqlEscape,sqlEscapeField
from DAL.organizationDAL import teamMembersDAL

org_teamMember = Blueprint('org_teamMember',__name__)


@org_teamMember.route("/org_teamMember/getlist",methods=['POST'])
def getlist():
    param = {
        'name': request.form.get('name') and sqlEscape(request.form.get('name')) or "",
        'department': request.form.get('department') and sqlEscape(request.form.get('department')) or None,
        'group': request.form.get('group') and sqlEscape(request.form.get('group')) or None,
        'jurisdiction': request.form.get('jurisdiction') and sqlEscape(request.form.get('jurisdiction')) or None,
        'pid': request.form.get('pid') and sqlEscape(request.form.get('pid')) or None,
        'page': request.form.get('page') and sqlEscape(request.form.get('page')) or 1,
        'limit': request.form.get('limit') and sqlEscape(request.form.get('limit')) or 20,
        'filed': request.form.get('filed') and sqlEscape(request.form.get('filed')) or 'id',
        'sort': request.form.get('sort') and sqlEscape(request.form.get('sort')) or 'ascending'
          }
    dal = teamMembersDAL()
    data,count = dal.getList(**param)
    # print(data,count)
    if data:
        res = getSuccessRes(data=data,count=count)
    else:
        res = getSuccessRes(data=[],count=count)
    return jsonify(res)


@org_teamMember.route("/org_teamMember/delete_by_id",methods=['POST'])
def delete_by_id():
    param = {
        'id': request.form.get('id') or None
    }
    if not param['id']:
        res = getErrorRes("你输入的参数有错！")
        return jsonify(res)
    dal = teamMembersDAL()
    data = dal.deleteById(**param)
    res = getSuccessRes(data=data)
    return jsonify(res)

@org_teamMember.route("/org_teamMember/update",methods=['POST'])
def update():
    param = {
        'id': request.form.get('id') or None,
        'name': request.form.get('name') and sqlEscape(request.form.get('name')) or "",
        'department': request.form.get('department') and sqlEscape(request.form.get('department')) or "",
        'group': request.form.get('group') and sqlEscape(request.form.get('group')) or "",
        'jurisdiction': request.form.get('jurisdiction') and sqlEscape(request.form.get('jurisdiction')) or "",
        'pid': request.form.get('pid') and sqlEscape(request.form.get('pid')) or "",
        'equipment': request.form.get('equipment') and sqlEscape(request.form.get('equipment')) or ""
    }
    if not param['id']:
        res = getErrorRes("参数id必须填写！")
        return jsonify(res)
    if not param['name']:
        res = getErrorRes("姓名必须填写！")
        return jsonify(res)
    if param['equipment']!="":
        param['equipment'] = param['equipment'].split(',')
    else:
        param['equipment']=[]
    dal = teamMembersDAL()
    data = dal.update(**param)
    res = getSuccessRes(data=data)
    return jsonify(res)

@org_teamMember.route("/org_teamMember/notExistAdd",methods=['POST'])
def add():
    param = {
        'id': request.form.get('id') or None,
        'name': request.form.get('name') and sqlEscape(request.form.get('name')) or "",
        'department': request.form.get('department') and sqlEscape(request.form.get('department')) or "",
        'group': request.form.get('group') and sqlEscape(request.form.get('group')) or "",
        'jurisdiction': request.form.get('jurisdiction') and sqlEscape(request.form.get('jurisdiction')) or "",
        'pid': request.form.get('pid') and sqlEscape(request.form.get('pid')) or "",
        'equipment': request.form.get('equipment') and sqlEscape(request.form.get('equipment')) or ""
    }
    if not param['name']:
        res = getErrorRes("姓名必须填写！")
        return jsonify(res)
    if param['equipment']!="":
        param['equipment'] = param['equipment'].split(',')
    else:
        param['equipment']=[]
    dal = teamMembersDAL()
    data = dal.notExistAdd(**param)
    res = getSuccessRes(data=data)
    return jsonify(res)

@org_teamMember.route("/org_teamMember/bindAccount",methods=['POST'])
def bindAccount():
    param = {
        'id': request.form.get('id') or None,
         'bind_account': request.form.get('bind_account') and sqlEscape(request.form.get('bind_account')) or ""
    }
    dal = teamMembersDAL()
    data = dal.bindAccount(**param)
    res = getSuccessRes(data=data)
    return jsonify(res)

@org_teamMember.route("/org_teamMember/getBoss",methods=['POST'])
def getBoss():
    dal = teamMembersDAL()
    data = dal.getBoss()
    if data:
        res = getSuccessRes(data=data)
    else:
        res = getSuccessRes(data=[])
    return jsonify(res)

